<?php

/**
 * @author DeineAgentur UG
 * @copyright 2010
 */

/**
* @author Rob Apodaca <rob.apodaca@gmail.com>
* @copyright Copyright (c) 2009, Rob Apodaca
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class autoloader
{
    /**
* Store the registered directories
* @var array
*/
    private $_directories   =   array();
    private $_fileformat    =   array(
                                    '%s_Controller.php',
                                    '%s_hclass.php',
                                    '%s.class.php',
                                    'class.%s.php',
                                );
    /**
* Class Constructor
*/
    public function __construct()
    {
        //Anytime an undefined class is called, php will invoke
        // AutoLoader::autoLoad()
        spl_autoload_register( array($this, 'autoLoad') );
    }

    /**
* The path the the directory to register (use trailing slash)
* @param string $dir
*/
    public function registerDirectory( $dir )
    {
        $this->_directories[] = realpath($dir).DS;
    }

    /**
* The path the the directory to register (use trailing slash)
* @param string $dir
*/
    public function registerFileformat( $format )
    {
        $this->_fileformat[] = $format;
    }

    /**
* Name of class to auto load (called by spl_autoload)
* @param string $class
* @return bool
*/
    public function autoLoad( $class )
    {
        $ixx=0;
        foreach( $this->_directories as $directory )
        {
            foreach($this->_fileformat as $fileNameFormat){
                $file = $directory.sprintf( $fileNameFormat , $class );
                if(file_exists($file)){
                    include_once($file);
                    return;
                }  
            }
        }
        return FALSE;
    }
}

function exception_handler($exception) {
ob_start();
print_r($GLOBALS);
print_r($exception);
  file_put_contents('../logs/exception'.date("d.m.Y_H:i:s").'.txt', ob_get_clean(). "\n",FILE_APPEND);
}
set_exception_handler('exception_handler'); 

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            $errors = "Notice";
            return true;
            break;
        case E_WARNING:
        case E_USER_WARNING:
            $errors = "Warning";
            break;
        case E_ERROR:
        case E_USER_ERROR:
            $errors = "Fatal Error";
            break;
        default:
            $errors = "Unknown";
            break;
        }

    if (ini_get("display_errors"))
        printf ("<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br /><br />\n", $errors, $errstr, $errfile, $errline);
    if (ini_get('log_errors'))
        error_log(sprintf("PHP %s:  %s in %s on line %d", $errors, $errstr, $errfile, $errline));
    return true;
}

// set to the user defined error handler
set_error_handler("myErrorHandler");

?>